
$(document).ready(function(){
	initMySummernote("remark");//初始化文本框
	getOrderList("",1,"","","");
});


/**
 * 获取订单列表
 */
function getOrderList(paramValue,currentPage,status,uptDateTime){
	$.ajax({
		url: urlcore + "/api/order/selectOvertimeList?param="+paramValue+"&current="+currentPage+"&status="+status+"&uptDateTime="+uptDateTime,
		type: "get",
		data: {},
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
	       	var da=data.data.page.records;
	       	var type=data.data.roleType;
	       	var status='none';
	       	if(type==0 || type==4){
	       		status='';
	       	}
               	var html="";
               	for(var i=0;i<da.length;i++){
               		var oweRentMoney = da[i].oweRentMoney;
               		var oweRentTime = da[i].oweRentTime;
               		var overtimeMoney = da[i].overtimeMoney;
               		var refundTime = "无"
               		if(da[i].order.status!='已完结'){
               			refundTime = new Date(da[i].refundTime).pattern("yyyy-MM-dd");
               		}
	               	if (oweRentMoney == null) {
	               		oweRentMoney = 0;
	               		oweRentTime = 0;
	               		overtimeMoney = 0;
	               	}
               		html+=  " <tr>\n" +   
               		        " <td>" + da[i].realName  + "</td>\n" +
				            " <td>" + da[i].phone  + "</td>\n" +
				            " <td>" + da[i].order.orderNumber  + "</td>\n" +
				            " <td>" + da[i].rentMoney  + "</td>\n" +
				            " <td>" + da[i].deposit  + "</td>\n" +
				            " <td>" + despositStatus(da[i].order.depositStatus)  + "</td>\n" +
				            " <td>" + da[i].dayRent  + "</td>\n" +
				            " <td>" + da[i].totalDays  + "</td>\n" +
				            " <td>" + oweRentMoney + "</td>\n" +
				            " <td>" + oweRentTime + "</td>\n" +
				            " <td>" + overtimeMoney + "</td>\n" +
				            " <td>" + da[i].oweTotalMoney + "</td>\n" +
				            " <td>" + refundTime  + "</td>\n" +
				            " <td>" +  da[i].order.status + "</td>\n" +
			                " <td>" +  da[i].loginName + "</td>\n" +
				            " <td>\n";
				            if(da[i].order.status=='已完结'){
				            	if(da[i].order.depositStatus==1){
//          		 html+=  "<a  class=\"btn btn-info btn-xs\" href=\"#\" onclick=\"paymoney("+da[i].order.id+","+da[i].userId+")\" >退还押金</a>&nbsp;"; 
				            	}else if(da[i].order.depositStatus==2){
//				     html+=  "<a  class=\"btn btn-info btn-xs\" href=\"overtime_record.html?orderId="+da[i].order.id+"\">逾期记录</a>&nbsp;";       		
				            	}
				            }else{
				     html+="<a  class=\"btn btn-success btn-xs\" href=\"javascript:;\" data-toggle=\"modal\" data-target=\"#update\" onclick=\"remarkBefore("+da[i].order.id+")\" >催款备注</a>&nbsp"+
                           "<a  class=\"btn btn-info btn-xs\" href=\"javascript:;\" style=\"display:"+status+";\"  onclick=\"payOrder("+da[i].order.id+","+da[i].userId+","+oweRentTime+")\" >线下付租</a>";
//				            "<a  class=\"btn btn-info btn-xs\" href=\"javascript:;\" style=\"display:"+status+";\"  onclick=\"orderOver("+da[i].order.id+")\" >订单完结</a>";
				            }
				     html+= " </td>"+ 
				            " </tr>";
               	}
               	$("#thislist").html(html);
               	$("#thiscount").html(data.data.page.total);
	          	$("#pager").pager({
					pagenumber: currentPage,
					pagecount: data.data.page.pages,
					totalcount: data.data.page.total,
					buttonClickCallback: PageClick
				});
			
			}else {
				swal(data.msg);
			}
		},
		error: function() {
			alert("服务器异常");
		}

	});
}

 PageClick = function(pageclickednumber) {
 	    var uptDateTime=$("#applyTime").val();
 	    var param=$("#param").val();
 	    var status=$("#status").val();
		getOrderList(param,pageclickednumber,status,uptDateTime);
	}
 
 

/**
 * 搜索
 */
function searchList(){
	var uptDateTime=$("#applyTime").val();
	var param=$("#param").val();
    var status=$("#status").val();
	getOrderList(param,1,status,uptDateTime);
}

/**
 *催款备注
 * @param {Object} id
 */
function remarkBefore(id){
	$('#orderId').attr('name',id);
	$.ajax({
		url: urlcore + "/api/order/selectOne?id=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			$('#remark').summernote("code",data.data.remark);
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
	
}

/**
 * 保证金状态
 */
function despositStatus(n){
	var status="";
	switch(n){
		case 1:
			status='未退回';
			break;
		case 2:
			status='已扣除';
			break;
		case 3:
			status='已退还';
			break;
		case 4:
			status='出款中';
			break;
		case 5:
			status='退款失败';
			break;
	}
	
	return status;
}

/**
 * 退还押金
 * @param {Object} id
 */
/*function paymoney(id,userId){
	swal({
			title: "退还金额",
			text: "请填写退还金额并确认",
			type: "input",
			showCancelButton: true,
			closeOnConfirm: false,
			animation: "slide-from-top",
			inputPlaceholder: "请保留两位小数"
		}, function(inputValue) {
			if (inputValue === false)
				return false;
			if (inputValue === "" || inputValue.indexOf(".")==-1) {
				swal.showInputError("金额不合法");
				return false
			}
			
		$.ajax({
			url: urlcore + "/api/yibao/transferSingle?orderId=" + id+"&money="+encode64(inputValue)+"&userId="+userId,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
			  if(data.success == true){
		          window.location.reload();
				}else{
					swal("OMG!", data.msg, "error");
				}
			 }
	     });
	});
}*/


/**
 * 
 * 线下付租
 */

 
function payOrder(id,userId,days) {  
	swal({
			title: "线下付租",
			text: "待付租情况下需要在金额后面添加#天数",
			type: "input",
			showCancelButton: true,
			closeOnConfirm: false,
			animation: "slide-from-top",
			inputPlaceholder: "请保留两位小数"
		}, function(inputValue) {
			if (inputValue === false)
				return false;
			if (inputValue === "" || inputValue.indexOf(".")==-1) {
				swal.showInputError("金额不合法");
				return false
			}
			if(days!=0 && inputValue.indexOf("#")!=-1){
				swal.showInputError("金额输入有误");
				return false
			}
			if(days==0 && inputValue.indexOf("#")==-1){
				swal.showInputError("金额输入有误");
				return false
			}
			submit(id,inputValue,userId);
	});
}


var checkSubmitFlg = false; 
function submit(id,inputValue,userId){
	 if (checkSubmitFlg == true) {  
            swal("您是请求已在处理中....");
            return false;  
        }  
        checkSubmitFlg = true; 
        $.ajax({
			url: urlcore + "/api/order/offlinePayorder?orderId=" + id+"&money="+encode64(inputValue)+"&userId="+userId,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
			  if(data.success == true){
		          window.location.reload();
				}else{
					swal("OMG!", data.msg, "error");
				}
			 }
	     });
         setTimeout(function() {  
        checkSubmitFlg = false;  
		}, 2000);  
    return true;  
}



function updateRemark(id) {
	var remark = $('#remark').summernote('code');
	if(confirm("确定？")) {
		$.ajax({
			url: urlcore + "/api/order/updateOrderRemark?id=" + id+"&remark="+remark,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				 var uptDateTime=$("#applyTime").val();
 	             var param=$("#param").val();
				 getOrderList(param,1,"",uptDateTime);
			},
			error: function() {
				/* Act on the event */
				alert("error");
			}
		});
	}
 
}

/**
 * 手动完结订单
 * @param {Object} id
 */
function orderOver(id){
	if(confirm("确定完结订单？")) {
		$.ajax({
			url: urlcore + "/api/evaluation/orderOver?id=" + id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				location.reload()
			},
			error: function() {
				/* Act on the event */
				alert("error");
			}
		});
	}
}


//导出表格
function excal() {
	var uptDateTime=$("#applyTime").val();
	var param=$("#param").val();
    var status=$('#status').val();
    if(status==''){
    	status="催收";
    }
	$.ajax({
		url: urlcore + "/api/order/makeExcle?param="+param+"&uptDateTime+"+uptDateTime+"&status="+status,
		type: "GET",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var url1 =  data.data.toString().slice(4);
				location.href="https"+url1;
			} else {
				alert(data.msg);
			}
		},
		error: function() {
			alert("error");
		}
	});
}
